package com.iot.device.utils;

import com.iot.common.core.domain.entity.SysDept;
import com.iot.common.core.redis.RedisCache;
import com.iot.common.utils.spring.SpringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ClientRedisUtils {

    private static final Logger logger = LoggerFactory.getLogger(ClientRedisUtils.class);
    // 设备在线集合key
    public static final String CLIENT_ONLINE_KEY = "eqClient:online";
    // 设备离线集合key
    public static final String CLIENT_OFFLINE_KEY = "eqClient:offline";
    // 设备编号+单位key
    public static final String CLIENT_NO_KEY = "eqClient:%s:%s";/*client:单位:终端编号*/

    //  系统单位数据 set
    public static final String EQ_SYS_DEPT = "eqSysDept"; /*eqSysDept*/
    public static final String EQ_SYS_DEPT_FORMAT = "%d:%d"; /*id:pid*/
    //
    // 行政区码数据，region_info  370000:山东
    public static final String REGION_INFO_DATA = "region_info";
    public static final String REGION_INFO_DATA_KEY = "%s:%s";

    public static void updateRedisSysDept(List<SysDept> sysDeptList) {
        RedisCache redisCache = SpringUtils.getBean(RedisCache.class);
        redisCache.deleteObject(ClientRedisUtils.EQ_SYS_DEPT);
        setRedisSysDept(sysDeptList);
    }

    public static void setRedisSysDept(List<SysDept> sysDeptList) {
        Map<String, SysDept> map = new HashMap<>();
        for(SysDept sysDept : sysDeptList){
            map.put(String.format(ClientRedisUtils.EQ_SYS_DEPT_FORMAT, sysDept.getDeptId(), sysDept.getParentId()), sysDept);
        }
        RedisCache redisCache = SpringUtils.getBean(RedisCache.class);
        redisCache.setCacheMap(ClientRedisUtils.EQ_SYS_DEPT, map);
    }
}
